package com.lee9213.mybatis.maven.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * mybatis生成器
 *
 * @author lee9213@163.com
 * @version 1.0
 * @date 2017/3/29 17:04
 */
@Mojo(name = "generator")
public class MybatisGeneratorPlugin extends AbstractMojo {

    @Parameter(defaultValue = "true")
    private boolean overwrite;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        try {
            URL url = this.getClass().getClassLoader().getResource("generator/generatorConfig.xml");
            Configuration config = cp.parseConfiguration(url.openStream());
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
